package com.chilly.jdkNio.action;

import io.netty.util.CharsetUtil;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 应用实例1：本地文件读数据
 * 读取文件filte01.txt中内容
 * Created by Chilly Cui on 2021/1/9.
 */
public class NIOFileChannelOper02 {
    public static void main(String[] args) throws IOException {
        File file = new File("c:\\env\\file01.txt");
        FileInputStream in = new FileInputStream(file);

        FileChannel fileChannel = in.getChannel();

        ByteBuffer byteBuffer = ByteBuffer.allocate((int) file.length());
        fileChannel.read(byteBuffer);

        System.out.println(new String(byteBuffer.array(), CharsetUtil.UTF_8));

        in.close();
        fileChannel.close();
    }
}
